package com.gs.api.util.video.parse.kuaishou;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.gs.api.util.video.parse.ImageParse;
import com.gs.api.util.video.parse.ParseConstant;
import com.gs.api.util.video.parse.ParseUtil;
import com.gs.api.util.video.parse.vo.VideoVo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.List;

/**
 * @author guoyunfei on 2022/4/14 16:06
 * @className KuaishouApp
 * @description TODO 快手APP
 * @Version 1.0
 */
public class KuaishouApp {

    /**
     * 快手图集，视频地址
     * @param url
     */
    public static VideoVo kuaiShouParseUrl(String url) {

        VideoVo videoVo = new VideoVo();

        try {
            String redirectUrl = HttpUtil.createGet(url).addHeaders(ParseUtil.headers()).execute().header("Location");

            String body = HttpUtil.createGet(redirectUrl).addHeaders(ParseUtil.headers()).execute().body();
            Document doc = Jsoup.parse(body);
            Elements videoElement = doc.select("script[type=text/javascript]");
            String videoInfo = videoElement.get(3).data().replaceAll("window.pageData= ", "");

            // 获取当前视频用户信息
            JSONObject obj = JSONObject.parseObject(videoInfo);

            // 获取当前视频信息
            JSONObject video = obj.getJSONObject("video");
            if("video".equals(video.getString("type"))){
                String videoSrc = kuaiShouParseUrl(obj);
                videoVo.setVideoSrc(videoSrc);
                videoVo.setType(ParseConstant.PARSE_VIDEO);

                String audio =  obj.getJSONArray("user_videos").getJSONObject(3).getJSONObject("soundTrack").getJSONArray("audioUrls").getJSONObject(0).getString("url");
                videoVo.setAudioSrc(audio);
            }else if("images".equals(video.getString("type"))){
                List<String> images = ImageParse.kuaiShouParseUrl(obj);
                videoVo.setImages(images);
                videoVo.setType(ParseConstant.PARSE_IMAGES);

                //获取音频地址
                String imageCDN = video.getString("imageCDN");
                String audio = video.getString("audio");
                videoVo.setAudioSrc(ParseConstant.HTTPS + imageCDN + audio);
            }

            //获取封面
            String poster = video.getString("poster");
            videoVo.setCoverSrc(poster);

            //文案
            String caption = video.getString("caption");
            videoVo.setCopywriting(caption);
        }catch (Exception e){
            e.printStackTrace();
            videoVo.setStatus(500);
        }

        return videoVo;
    }


    /**
     * 快手视频
     */
    public static String kuaiShouParseUrl(JSONObject obj) {
        // 获取当前视频信息
        JSONObject video = obj.getJSONObject("video");
        // 无水印视频地址
        String videoUrl = video.getString("srcNoMark");
        // 截取前面的数据
        videoUrl = videoUrl.substring(0, videoUrl.indexOf("?"));
        System.out.println("快手视频原地址：" + videoUrl);
        return videoUrl;
    }

}
